package com.kong.meirimall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.kong.meirimall.product.entity.ProductAttrValueEntity;
import com.kong.meirimall.product.service.ProductAttrValueService;
import com.kong.meirimall.product.vo.AttrResponseVo;
import com.kong.meirimall.product.vo.AttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.kong.meirimall.product.service.AttrService;
import com.kong.common.utils.PageUtils;
import com.kong.common.utils.R;



/**
 * 商品属性
 *
 * @author kong
 * @email kong@gmail.com
 * @date 2024-10-03 18:13:07
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;

    @Autowired
    ProductAttrValueService productAttrValueService;

    ///api/product/attr/base/listforspu/29
    @GetMapping("/base/listforspu/{spuId}")
    public R baseAttrListforspu(@PathVariable Long spuId){
        List<ProductAttrValueEntity> attrEntities=productAttrValueService.baseAttrListforspu(spuId);
        return R.ok().put("data",attrEntities);
    }

    // base 规格参数（基本属性）       sale 销售属性
    ///product/attr/base/list/{catlogId}
    @GetMapping("/{attrtype}/list/{catalogId}")
    public R baseList(@RequestParam Map<String,Object> params,
                      @PathVariable("catalogId") Long catalogId,
                      @PathVariable("attrtype") String type
                      ){
        PageUtils page=attrService.queryPageAttr(params,catalogId,type);
        return R.ok().put("page",page);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:attr:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    //@RequiresPermissions("product:attr:info")
    public R info(@PathVariable("attrId") Long attrId){
//		AttrEntity attr = attrService.getById(attrId);
        AttrResponseVo respVo = attrService.geAttrInfo(attrId);
        return R.ok().put("attr", respVo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attr:save")
    public R save(@RequestBody AttrVo attrVo){
		attrService.saveAttr(attrVo);

        return R.ok();
    }

    //指定 spuId 修改规格属性
    @PostMapping("/update/{spuId}")
    public R updateSquAttr(@PathVariable Long spuId, @RequestBody List<ProductAttrValueEntity> entities){
        productAttrValueService.updateSpuAttr(spuId,entities);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attr:update")
    public R update(@RequestBody AttrVo attrVo){
		attrService.updateAttr(attrVo);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attr:delete")
    public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
